package com.feiyun.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyun.common.PageParam;
import com.feiyun.model.dto.ResultVO;
import com.feiyun.model.entity.UploadRecordInfo;
import com.feiyun.model.entity.UserPhoto;
import com.feiyun.service.IFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;

import static com.feiyun.util.ResponseUtil.exception;
import static com.feiyun.util.ResponseUtil.success;

/**
 * Function Desc: FileController
 *
 * @author yan
 * @since 2023/3/28 22:15
 */
@Controller
@RequestMapping("file")
@Tag(name = "文件处理", description = "包括头像、照片")
@Slf4j
public class FileController {
    @Resource
    private IFileService fileService;

    /*------------------------------------------------图片jpg文件处理---------------------------------------------------*/
    @PostMapping("photo/upload")
    @Operation(summary = "批量文件上传")
    public ResponseEntity<ResultVO<List<UserPhoto>>> batchUploadPhoto(
            @ModelAttribute @Valid UserPhoto userPhotoEntity,
            @RequestParam("files") List<MultipartFile> files) {
        log.info("调用批量文件上传接口,传递参数：{}", JSONObject.toJSONString(userPhotoEntity));
        return success(fileService.uploadPhotos(userPhotoEntity, files));
    }

    @PostMapping("photo/delete")
    @Operation(summary = "批量删除照片")
    public ResponseEntity<ResultVO<Void>> batchDeletePhoto(@RequestBody List<Integer> userPhotoIds) {
        log.info("调用批量删除照片接口,传递参数：{}", JSONObject.toJSONString(userPhotoIds));
        fileService.deletePhotos(userPhotoIds);
        return success();
    }

    @PostMapping("photo/select")
    @Operation(summary = "分类查找用户照片")
    public ResponseEntity<ResultVO<List<UserPhoto>>> selectUserPhotoByCondition(@RequestBody UserPhoto userPhotoEntity) {
        log.info("调用分类查找用户照片接口,传递参数：{}", JSONObject.toJSONString(userPhotoEntity));
        return success(fileService.selectUserPhotoByCondition(userPhotoEntity));
    }

    /*------------------------------------------------Excel文件处理----------------------------------------------------*/
    @PostMapping("excleUpload")
    @Operation(summary = "excle客户信息上传")
    public ResponseEntity<ResultVO<String>> excleUpload(
            @RequestParam("file") @NotNull(message = "无法上传空文件") MultipartFile file) {
        log.info("调用excle客户信息上传接口");
        if (fileService.excleUpload(file)) {
            return success("上传成功，解析完成");
        } else {
            return exception("解析失败，请检查文件", null);
        }
    }

    @PostMapping("finduploadInfoList")
    @Operation(summary = "excle上传记录列表")
    public ResponseEntity<ResultVO<List<UploadRecordInfo>>> finduploadInfoList(Model model) {
        log.info("调用获取excle上传记录列表接口,传递参数：{}", JSONObject.toJSONString(model));
        List<UploadRecordInfo> uploadRecordInfos = fileService.finduploadInfoList();
        model.addAttribute("uploadInfoList", uploadRecordInfos);
        return success(uploadRecordInfos);
    }

    @PostMapping("finduploadInfo")
    @Operation(summary = "excle上传记录列表")
    public ResponseEntity<ResultVO<Page<UploadRecordInfo>>> finduploadInfo(@RequestBody PageParam<UploadRecordInfo> userPageParam) {
        log.info("调用获取excle上传记录列表接口,传递参数：{}", JSONObject.toJSONString(userPageParam));
        return success(fileService.finduploadInfo(userPageParam));
    }

    @PostMapping("approvedUploadInfo")
    @Operation(summary = "通过客户信息")
    public ResponseEntity<ResultVO<Void>> updateUploadInfo(@RequestBody UploadRecordInfo uploadRecordInfo) {
        fileService.updateUploadInfo(uploadRecordInfo);
        return success();
    }

    @GetMapping("downTemplate")
    @Operation(summary = "exdownTemplatecle模板下载")
    public void downTemplate(HttpServletRequest request, HttpServletResponse response) {
        fileService.downTemplate(request, response);
    }

    @GetMapping("deleteUploadRecordById/{id}")
    @Operation(summary = "删除上传excle记录")
    public ResponseEntity<ResultVO<Void>> deleteUploadRecordById(@PathVariable @PositiveOrZero String id) {
        fileService.deleteUploadRecordById(id);
        return success();
    }

    @GetMapping("deletePictureById/{id}")
    @Operation(summary = "用户删除上传图片")
    public ResponseEntity<ResultVO<Void>> deletePictureById(@PathVariable @PositiveOrZero String id) {
        log.info("调用用户删除上传图片接口,传递参数：{}", JSONObject.toJSONString(id));
        fileService.deletePictureById(id);
        return success();
    }
}
